package com.goldgov.conference.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.conference.dao.ConferenceDao;
import com.goldgov.conference.service.Conference;
import com.goldgov.conference.service.ConferenceBean;
import com.goldgov.conference.service.ConferenceQuery;
import com.goldgov.conference.service.ConferenceService;
import com.goldgov.conference.service.ConferenceUser;

/** 
 * @ClassName: ConferenceServiceImpl 
 * @Description: TODO
 * @author hanxiangyu
 * @date 2018年6月4日 上午10:17:49 
 */
@Service
public class ConferenceServiceImpl implements ConferenceService {

	@Autowired
	private ConferenceDao conferenceDao;
	
	
	/** 
	 * @Description: 添加会议记录实现
	 */
	@Override
	public Boolean addConference(Conference conference) {
		Boolean fals = true;
		int state = conferenceDao.addConference(conference);//会议记录
		if (state >= 1) {
			fals = true;
		}else {
			fals = false;
		}
		return fals;
	}

	/** 
	 * @Description: 添加会议记录与用户关系表
	 * @author hanxiangyu
	 * @date 2018年6月6日 下午12:01:56 
	 */
	@Override
	public Boolean addConferenceUser2(ConferenceUser con) {
		Boolean fals = true;
		List<ConferenceUser> conferenceUser = new ArrayList<>();
		for (String id : con.getUserIDs()) {
			ConferenceUser cu = new ConferenceUser();
			cu.setUserID(id);
			cu.setConferenceID(con.getConferenceID());
			cu.setConferenceUserID(getConferenceID().getConferenceID());
			cu.setSignDate(new Date());
			conferenceUser.add(cu);
		}
		//更新总人数
//		Conference conference = new Conference();
//		conference.setConferenceID(con.getConferenceID());
//		conference.setHeadNum(con.getPeopleNum());
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		conference.setLastModifiedDate(sdf.format(new Date()));
//		conferenceDao.updateConference(conference);
		//添加会议与用户关系
		if(null!=conferenceUser && conferenceUser.size()>0) {
			int state = conferenceDao.addConferenceUser(conferenceUser);
			if (state >= 1) {
				fals = true;
			} else {
				fals = false;
			}
		}
		return fals;
	}
	/** 
	 * @Description: 添加会议记录与用户关系表
	 * @author hanxiangyu
	 * @date 2018年6月6日 下午12:01:56 
	 */
	@Override
	public Boolean addConferenceUser(List<ConferenceUser> list) {
		Boolean fals = true;
		int state = conferenceDao.addConferenceUser(list);
		if (state >= 1) {
			fals = true;
		} else {
			fals = false;
		}
		return fals;
	}
	
	/** 
	 * @Description: 预新增
	 * @author hanxiangyu
	 * @date 2018年6月4日 上午10:38:43 
	 */
	@Override
	public Conference getConferenceID() {
		return conferenceDao.getConferenceID();
	}


	/**
	 * 获取会议记录列表
	 * @param query
	 * @return
	 */
	@Override
	public List<Conference> getConferenceList(ConferenceQuery query) {
		List<Conference> list = conferenceDao.getConferenceList(query);
		for (Conference conference : list) {
			Conference c = new Conference();
			c.setConferenceID(conference.getConferenceID());
			c.setUserID(query.getSearchUserId());
			Conference userStates = conferenceDao.getUserStates(c);
			if (userStates == null) {
				conference.setUserStatus(3);	//未参加
			}else {
				conference.setUserStatus(userStates.getUserStatus());
			}
		}
		return list;
	}
	/**
	 * 获取会议记录列表管理员
	 * @param query
	 * @return
	 */
	@Override
	public List<Conference> getConferenceListByAdmin(ConferenceQuery query){
		List<Conference> conferenceList = conferenceDao.getConferenceListByAdmin(query);
		for (Conference conference : conferenceList) {
			Conference c = new Conference();
			c.setConferenceID(conference.getConferenceID());
			c.setUserID(query.getSearchUserId());
			Conference userStates = conferenceDao.getUserStates(c);
			if (userStates == null) {
				conference.setUserStatus(3);	//未参加
			}else {
				conference.setUserStatus(userStates.getUserStatus());
			}
		}
		return conferenceList;
		
	}

	@Override
	public List<Conference> getConferenceListMobileByAdmin(ConferenceQuery query) {
		List<Conference> conferenceList = conferenceDao.getConferenceListMobileByAdmin(query);
		for (Conference conference : conferenceList) {
			Conference c = new Conference();
			c.setConferenceID(conference.getConferenceID());
			c.setUserID(query.getSearchUserId());
			Conference userStates = conferenceDao.getUserStates(c);
			if (userStates == null) {
				conference.setUserStatus(3);	//未参加
			}else {
				conference.setUserStatus(userStates.getUserStatus());
			}
		}
		return conferenceList;
	}
	/**
	 * 根据主键查询会议记录
	 * @param id
	 * @return
	 */
	public Conference getConference(Conference obj) {
		Conference conference = conferenceDao.getConference(obj.getConferenceID());
		Conference userStates = conferenceDao.getUserStates(obj);
		if (userStates == null) {
			conference.setUserStatus(ConferenceQuery.THREE);	//未参加
		}else {
			conference.setUserStatus(userStates.getUserStatus());
		}
		return conference;
	}
	
	
	
	/** 
	 * @Description: 删除会议记录
	 * @author hanxiangyu
	 * @date 2018年6月5日 下午6:59:09 
	 */
	@Override
	public Boolean deleteConference(String[] ids) {
		Boolean fals = true;
		int state = conferenceDao.deleteConference(ids);
		if (state >= 1) {
			fals = true;
		} else {
			fals = false;
		}
		return fals;
	}


	/** 
	 * @Description: 更新会议记录
	 * @author hanxiangyu
	 * @date 2018年6月6日 上午11:19:41 
	 */
	@Override
	public Boolean updateConference(Conference conference) {
		Boolean fals = true;
		int state = conferenceDao.updateConference(conference);
		if (state >= 1) {
			fals = true;
		} else {
			fals = false;
		}
		return fals;
	}


	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月6日 上午11:19:41 
	 */
	@Override
	public Boolean deleteConferenceCorrelation(String conferenceID,String[] conferenceIDs) {
		Boolean fals = true;
		int state = conferenceDao.deleteConferenceAttendance(conferenceID,conferenceIDs);
		conferenceDao.deleteConferenceUser(conferenceID,conferenceIDs);
		if (state >= 1) {
			fals = true;
		} else {
			fals = false;
		}
		return fals;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月6日 下午8:12:48 
	 */
	@Override
	public List<ConferenceUser> getExistUserIDs(String[] ids, String conferenceID) {
		return conferenceDao.getExistUserIDs(ids, conferenceID);
	}

	
	/**
	 * 取消会议记录
	 * @param ids
	 */
	public Boolean cancelConference(String[] conferenceIDs,Integer activeState) {
		Boolean fals = true;
		int state = conferenceDao.cancelConference(conferenceIDs, activeState);
		if (state >= 1) {
			fals = true;
		} else {
			fals = false;
		}
		return fals;
	}

	@Override
	public Boolean cancelConferenceByID(String conferenceID,Integer publishState) {
		Boolean fase = false;
		int state = conferenceDao.cancelConferenceByID(conferenceID,publishState);
		if (state > 0) {
			fase = true;
		} 
		return fase;
	}

	/**
	 * 发布取消会议操作
	 * @date 2018年6月11日 上午11:57:05 
	 * @return
	 */
	@Override
	public Boolean publishConference(Conference conference) {
		Boolean fals = true;
		int state = conferenceDao.publishConference(conference);
		if (state >= 1) {
			fals = true;
		} else {
			fals = false;
		}
		return fals;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月20日 下午7:22:22 
	 */
	@Override
	public int updateNum(Conference conference) {
		return conferenceDao.updateNum(conference);
	}
	@Override
	public Boolean activeConference(Conference conference) {
		Boolean fals = true;
		int state = conferenceDao.activeConference(conference);
		if (state >= 1) {
			fals = true;
		} else {
			fals = false;
		}
		return fals;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月20日 下午9:06:43 
	 */
	@Override
	public Conference getConferenceById(String id) {
		return conferenceDao.getConferenceById(id);
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月25日 下午3:16:29 
	 */
	@Override
	public Conference getUserStates(Conference conference) {
		return conferenceDao.getUserStates(conference);
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年7月8日 下午9:32:29 
	 */
	@Override
	public List<Conference> getConListByIds(String[] ids) {
		return conferenceDao.getConListByIds(ids);
	}

	@Override
	public Boolean startAndEndConference(ConferenceBean conferenceBean) {
		Boolean fals = true;
		int state = conferenceDao.startAndEndConference(conferenceBean);
		if (state >= 1) {
			fals = true;
		} else {
			fals = false;
		}
		return fals;
	}

	@Override
	public String getOrgIdByCode(String scopeCode) {
		return conferenceDao.getOrgIdByCode(scopeCode);
	}
	/**
	 * 根据userId获取组织结构id
	 * @param scopeCode
	 * @return
	 */
	public String getOrgIdByUserId(String userId) {
		return conferenceDao.getOrgIdByUserId(userId);
	}
	/**
	 * 根据支部id获取会议记录列表
	 * @param query
	 * @return
	 */
	public List<Conference> getConferenceListByOrgId(ConferenceQuery query){
		List<Conference> list = conferenceDao.getConferenceListByOrgId(query);
		for (Conference conference : list) {
			Conference c = new Conference();
			c.setConferenceID(conference.getConferenceID());
			c.setUserID(query.getSearchUserId());
			Conference userStates = conferenceDao.getUserStates(c);
			if (userStates == null) {
				conference.setUserStatus(3);	//未参加
			}else {
				conference.setUserStatus(userStates.getUserStatus());
			}
		}
		return list;
	}
	
}
